package com.acwing.partition12;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/1/18 11:54
 */
public class AC1140最短网络 {

    private static final int N = 110, INF = 0x3f3f3f3f;
    private static int[][] graph = new int[N][N];
    private static boolean[] visited = new boolean[N];
    private static int[] dist = new int[N];
    private static int n = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        for (int i = 0; i < n; i++) {
            String[] s = reader.readLine().split("\\s+");
            for (int j = 0; j < n; j++) {
                graph[i][j] = Integer.parseInt(s[j]);
            }
        }
        writer.write(prim(0) + "\n");
        writer.flush();
    }

    private static int prim(int start) {
        int answer = 0;
        Arrays.fill(dist, INF);
        dist[start] = 0;
        for (int k = 1; k <= n; k++) {
            int u = -1;
            for (int i = 0; i < n; i++) {
                if (!visited[i] && (u == -1 || dist[i] < dist[u])) u = i;
            }
            if (u == -1) return INF;
            answer += dist[u];
            visited[u] = true;
            for (int v = 0; v < n; v++) dist[v] = Math.min(dist[v], graph[u][v]);
        }
        return answer;
    }
}
